<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发货单号" prop="deliverId">
        <el-input
          v-model="queryParams.deliverId"
          placeholder="请输入发货单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="承运时间">
        <el-date-picker
          v-model="daterangeTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="车牌号" prop="carBrand">
        <el-input
          v-model="queryParams.carBrand"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="所属团长" prop="leader">
        <el-select
          v-model="queryParams.leader"
          placeholder="请选择驿站名称"
          clearable
          @change="handleQuery"
        >
          <!-- 下拉选项列表，可根据实际业务场景修改选项值 -->
          <el-option
            v-for="(item, index) in leaderOption"
            :key="index"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>



      <el-form-item label="运输司机" prop="driver">
        <el-select
          v-model="queryParams.driver"
          placeholder="请选择驿站名称"
          clearable
          @change="handleQuery"
        >
          <!-- 下拉选项列表，可根据实际业务场景修改选项值 -->
          <el-option
            v-for="(item, index) in driverOption"
            :key="index"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="运输状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择运输状态"
          clearable
          @change="handleQuery"
        >
          <!-- 下拉选项列表，可根据实际业务场景修改选项值 -->
          <el-option
            v-for="(item, index) in statusOption"
            :key="index"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>













      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['deliver:deliver:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['deliver:deliver:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['deliver:deliver:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['deliver:deliver:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deliverList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="发货单号" align="center" prop="deliverId" />
      <el-table-column label="承运时间" align="center" prop="time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属团长" align="center" prop="leader" />
      <el-table-column label="货物数量" align="center" prop="goodsNum" />
      <el-table-column label="运输司机" align="center" prop="driver" />
      <el-table-column label="车牌号" align="center" prop="carBrand" />
      <el-table-column label="运输状态" align="center" prop="status" >
        <template #default="scope">
          <el-tag v-if="scope.row.status==1">
            待发货
          </el-tag>
          <el-tag v-if="scope.row.status==2">
            送货中
          </el-tag>
          <el-tag v-if="scope.row.status==3">
            已发货
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['deliver:deliver:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['deliver:deliver:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改发货管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="承运时间" prop="time">
          <el-date-picker clearable
            v-model="form.time"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择承运时间">
          </el-date-picker>
        </el-form-item>


        <el-form-item label="所属团长">
          <el-select v-model="form.leader">
            <el-option label="团长A" value="团长A" />
            <el-option label="团长B" value="团长B" />
            <el-option label="团长C" value="团长C" />
            <el-option label="团长D" value="团长D" />
          </el-select>
        </el-form-item>



        <el-form-item label="运输司机">
          <el-select v-model="form.driver">
            <el-option label="A-司机" value="A-司机" />
            <el-option label="B-司机" value="B-司机" />
            <el-option label="C-司机" value="C-司机" />
            <el-option label="D-司机" value="D-司机" />
          </el-select>
        </el-form-item>


        <el-form-item label="运输状态">
          <el-select v-model="form.status">
            <el-option label="1" value="1" >
              <el-tag>待发货</el-tag>
            </el-option>
            <el-option label="2" value="2" >
              <el-tag>送货中</el-tag>
            </el-option>
            <el-option label="3" value="3" >
              <el-tag>已收货</el-tag>
            </el-option>
          </el-select>
        </el-form-item>



        <el-form-item label="货物数量" prop="goodsNum">
          <el-input v-model="form.goodsNum" placeholder="请输入货物数量" />
        </el-form-item>
        <el-form-item label="车牌号" prop="carBrand">
          <el-input v-model="form.carBrand" placeholder="请输入车牌号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDeliver, getDeliver, delDeliver, addDeliver, updateDeliver } from "@/api/deliver/deliver"

export default {
  name: "Deliver",
  data() {
    return {
      leaderOption: [
        { label: '团长A', value: '团长A' },
        { label: '团长B', value: '团长B' },
        { label: '团长C', value: '团长C' },
        { label: '团长D', value: '团长D' },
      ],

      driverOption: [
        { label: 'A-司机', value: 'A-司机' },
        { label: 'B-司机', value: 'B-司机' },
        { label: 'C-司机', value: 'C-司机' },
        { label: 'D-司机', value: 'D-司机' },
      ],

      statusOption: [
        { label: '待发货', value: '1' },
        { label: '送货中', value: '2' },
        { label: '已收货', value: '3' },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 发货管理表格数据
      deliverList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 运输状态时间范围
      daterangeTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deliverId: null,
        time: null,
        leader: null,
        driver: null,
        carBrand: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询发货管理列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeTime && '' != this.daterangeTime) {
        this.queryParams.params["beginTime"] = this.daterangeTime[0]
        this.queryParams.params["endTime"] = this.daterangeTime[1]
      }
      listDeliver(this.queryParams).then(response => {
        this.deliverList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        deliverId: null,
        time: null,
        leader: null,
        goodsNum: null,
        driver: null,
        carBrand: null,
        status: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeTime = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deliverId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加发货管理"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const deliverId = row.deliverId || this.ids
      getDeliver(deliverId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改发货管理"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.deliverId != null) {
            updateDeliver(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addDeliver(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deliverIds = row.deliverId || this.ids
      this.$modal.confirm('是否确认删除发货管理编号为"' + deliverIds + '"的数据项？').then(function() {
        return delDeliver(deliverIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('deliver/deliver/export', {
        ...this.queryParams
      }, `deliver_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
